<?php
class Controller{
	var $methods;
	var $layout="default";
	var $view="View";
	var $output=null;
	var $viewData=Array();
	var $viewObj;
	function __construct() {
		if ($this->name === null) {
			$r = null;
			if (!preg_match('/(.*)Controller/i', get_class($this), $r)) {
				throw new Exception("Controller::__construct() : Can not get or parse my own class name, exiting.");
			}
			$this->name = $r[1];
		}
		/*
		 if ($this->viewPath == null) {
			$this->viewPath = Inflector::underscore($this->name);
			}
			$this->modelClass = Inflector::classify($this->name);
			$this->modelKey = Inflector::underscore($this->modelClass);
			$this->Component =& new Component();*/

		$childMethods = get_class_methods($this);
		$parentMethods = get_class_methods('Controller');

		foreach ($childMethods as $key => $value) {
			$childMethods[$key] = strtolower($value);
		}

		foreach ($parentMethods as $key => $value) {
			$parentMethods[$key] = strtolower($value);
		}
		$this->methods = array_diff($childMethods, $parentMethods);
		$this->params["form"]=$_POST;
	}
	function constructClasses(){
		$module_list=$this->uses;
		foreach($module_list as $key => $model_name){
			$filename='./app/models/'.strtolower($model_name).'.php';
			load($filename);
			$classname=$model_name."Model";
			$this->{$model_name}=new $model_name($controller);
		}
	}
	function beforeFilter(){

	}
	function render($action = null, $layout = null, $file = null){
		if($this->view!="View"){
			$filename='./app/views/'.strtolower($this->view).'.php';
			load($filename);
			$classname=$this->view.'View';
			$view=new $classname($this); 
		}else{
			$view=new View($this); 
		}
		$this->Component->beforeRender($this);
		$view->viewVars=$this->viewData;
		$this->viewObj=$view;
		$view->layout=$this->layout;
		$this->output= $view->render($action,$layout,$file);
		return $this->output;
	}
	function set($key, $value){
		$this->viewData[$key]=$value;
	}
};